package com.linkoog.devtools.intellij.wizard;

import com.intellij.ide.wizard.AbstractWizard;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 *   向导式弹出框
 */
public class WizardDialog extends AbstractWizard<WizardStep> {

    private final Project project;

    @Getter
    private final WizardContext wizardContext;

    public WizardDialog(@NotNull Project project, @NotNull WizardContext wizardContext, @NotNull String title, WizardStep[] wizardSteps) {
        super(title, project);
        this.getHelpButton().setVisible(false);
        this.project = project;
        this.wizardContext = wizardContext;
        for (WizardStep wizardStep : wizardSteps) {
            addStep(wizardStep);
        }
        init();
    }

    @Override
    protected void doNextAction() {
        WizardStep step = getCurrentStepObject();
        try {
            if (step.validate()) {
                step.onStepOK();
                step.dispose();
                WizardStep nextStep = getNextStepObject();
                nextStep.onStepPrepare();
                super.doNextAction();
            }
        } catch (ConfigurationException e) {
            Messages.showErrorDialog(step.getComponent(), e.getMessage(), e.getTitle());
        }
    }

    @Override
    protected void doOKAction() {
        WizardStep step = getCurrentStepObject();
        try {
            if (step.validate()) {
                step.onStepOK();
                step.dispose();
                super.doOKAction();
            }
        } catch (ConfigurationException e) {
            Messages.showErrorDialog(step.getComponent(), e.getMessage(), e.getTitle());
        }
    }


    @Nullable
    @Override
    protected String getHelpID() {
        return WizardDialog.class.getName();
    }

}

